var title = "参与活动人员";
var config = {
    url_page: _dir + "lotteryManage.html",
    url_add: _dir + "lotteryManage_add.html",
    title_page: title + "查询",
    title_list: title + "列表",
    title_add: "添加" + title,
    title_edit: "修改" + title,
    code_list: "/lotteryCase/user/query",
    code_delete: "/lottery/del",
    code_add: "/lotteryCase/add",
    code_edit: "/lottery/update",
    code_dataUpload: "/lottery/coverUpload",
    code_imgUpload: "/lottery/upload",
    open_ward: "/lotteryCase/public",//开奖
    pagination_01: $("#page_01"),
    cancel_ward:'/lotteryCase/delWin'
};
$(document).ready(function () {
    if (_page == "Participation_List") {
        var _fid = $.trim($.request.queryString["fid"]);
        console.log('test',_fid)
        var _fstaffNo = $.trim($.request.queryString["id"]);
        var _rtype = $.trim($.request.queryString["rtype"]);
        var _msgId = 0;

        var pageNo = 1;
        setTitle_01(config['title_list'], config['title_page'], config['url_add']);
        setDefaultDate($("#startTime"),$("#endTime"))
        //加载事件
        var _trs, _pageSize, _pageCount;
        var setData = function (pIndex, pSize) {
            pIndex = pIndex || 1;
            pSize = pSize || 20;
            var pageSetBody = { "pageNo": pIndex, "pageSize": pSize };
            var sendObj = {
                //"bookCode": $("#bookCode").val(),
                //"bookName": $("#bookName").val(),
                //"startTime": $("#startTime").val(),
                //"endTime": $("#endTime").val(),
                'id':_fid,
                'winState':-1
            };
            window.sessionStorage.setItem(_page+"data",JSON.stringify({
                'sendObj':sendObj,
                'pageSetBody':pageSetBody
            }));
            $.extend(sendObj,pageSetBody);
            _call(config['code_list'], sendObj, function (res) {
                _trs = "";
                if (!res.body || res.body.length == 0) {
                    _trs = "";
                    _pageSize = 1;
                    _pageCount = 1;
                    pageNo--
                    d_alert('警告','没有更多数据啦！','warning')
                }
                else {
                    _pageSize = res.body.pageSize;
                    _pageCount = res.body.count;
                    $.each(res.body, function (i, v) {
                        if(v['winState']==1){
                            _trs = _trs + `<tr>
                            <td  style="display: none">${_fid}</td>
                            <td  style="display: none">${dataIsNull(v['clientUserId'])}</td>

                            <td>${dataIsNull(v['tel'])}</td>
                            <td>${dataIsNull(v['name'])}</td>

                            <td>${dataIsNull(v['addr'])}</td>


                            <td>${dataIsNull(v['isActivate'])==1?'已激活':'未激活'}</td>
                            <td>${dataIsNull(v['integral'])}</td>
                            <td>${dataIsNull(v['recommendCount'])}</td>
                            <td class="cancelZhong">${getTdOperate(11, config['open_ward'], v.clientUserId)}</td>

                            </tr>`;
                        }else{
                            _trs = _trs + `<tr>
                            <td  style="display: none">${_fid}</td>
                            <td  style="display: none">${dataIsNull(v['clientUserId'])}</td>

                            <td>${dataIsNull(v['tel'])}</td>
                            <td>${dataIsNull(v['name'])}</td>

                            <td>${dataIsNull(v['addr'])}</td>


                            <td>${dataIsNull(v['isActivate'])==1?'已激活':'未激活'}</td>
                            <td>${dataIsNull(v['integral'])}</td>
                            <td>${dataIsNull(v['recommendCount'])}</td>
                            <td class="settinBtn">${getTdOperate(8, config['open_ward'], v.clientUserId)}</td>

                            </tr>`;
                        }

                    });
                    setPageData(res.body);
                    $("#table_01 tbody").html(_trs);
                }

                //绑定删除事件
                setDelete("id", config['code_delete']);

                //分页方法
            });
        };

        var loadingAll = function () {
            //初始化加载数据
            setData();
        }

        autoSearchByCookie(loadingAll);
        //查询方法
        loadingAll();
        $(".homePage").click(function(){
            pageNo = 1
            setData(pageNo)
        })
        $(".prevPage").click(function(){
            if(pageNo > 1){
                pageNo--
                setData(pageNo)
            }else{
                d_alert("警告",'已经到头啦！','warning')
            }
            
        })
        $(".nextPage").click(function(){
            pageNo++
            setData(pageNo)
        })

        // 设为中奖
        $('#table_01').on('click','.settinBtn',function () {
            var clientUserId=$(this).parent().find('td').eq(1).text()
            var _this = $(this);
            setWard(config['open_ward'],{clientUserId:clientUserId,id:_fid},function () {
                //_this.parents("tr").remove();
                var userManagerdata=pageNo
                var loadingAll = function () {
                    //初始化加载数据
                    setData(userManagerdata);
                    console.log('执行了')
                }
                loadingAll()
            })

        })


        // 取消中奖
        $('#table_01').on('click','.cancelZhong .cancelZhongSmall',function () {
            var clientUserId=$(this).parents().find('td').eq(1).text()
            var _this = $(this);
            cancelWard(config['cancel_ward'],{clientUserId:clientUserId,id:_fid},function () {
              //  _this.parents("tr").remove();
                var userManagerdata=pageNo
                var loadingAll = function () {
                    //初始化加载数据
                    setData(userManagerdata);
                    console.log('执行了')
                }
                loadingAll()
            })

        })
        // 查看本期中奖名单
        $('#seeAgo').click(function () {
            location.href='winnerList.html?fid='+_fid
        })
    }
    else if (_page == "lottery_add") {
        $('.file-caption-main').css({
            display:'none'
        });
        var sendObj2 = {};

        //初始化
        var PageInit = function () {
            var _fid = $.trim($.request.queryString["fid"]);
            var _fstaffNo = $.trim($.request.queryString["id"]);
            var _rtype = $.trim($.request.queryString["rtype"]);
            var _msgId = 0;
            this.get_fid = function () {
                return this._fid;
            };
            this.get_msgId = function () {
                return this._msgId;
            };
            this.set_fid = function (v) {
                this._fid = v;
            }
            this.set_msgId = function (v) {
                this._msgId = v;
            }
            this.setDefault = function (v) {
                if (_fid == "" && _fstaffNo == "" && _rtype == "") {
                    //添加
                    this.set_msgId(config['code_add']);
                    this.set_fid(0);
                    setTitle_02(config['title_add'],config['url_page']);
                    new setUpload($("#idCardParent"),{'uploadUrl':config['code_dataUpload'],"urlDom":$("#coverUrl"),'title':"封面",'responseUrl':"coverUrl"});
                }
                else if (_fid != "" && _fstaffNo != "" && _rtype == "edit") {
                    //修改
                    this.set_msgId(config['code_edit']);
                    this.set_fid(_fid);
                    setTitle_02(config['title_edit'],config['url_page']);
                    sendObj2['id'] = _fid;

                    if (getPageData(_fstaffNo)) {
                        var _v = getPageData(_fstaffNo);
                        var formObj = new Form();
                        formObj.init(_v);
                        new setUpload($("#idCardParent"),{'uploadUrl':config['code_dataUpload'],"urlDom":$("#coverUrl"),'title':"封面",'responseUrl':"coverUrl"});

                    }
                }

            }
        };
        var _default = new PageInit();
        _default.setDefault();

        //提交事件
        $(".validate-form .submit").on("click", function () {
            var _this = $(this);
            if ($(".validate-form").validate().form()) {
                sendObj2["name"] = $("#name").val();
                sendObj2["bookDescribe"] = $("#bookDescribe").val();
                sendObj2["url"] = $("#url").val();
                // sendObj2["downloadUrl"] = $("#downloadUrl").val();
                sendObj2["startTime"] = $("#startTime").val();
                sendObj2["endTime"] = $("#endTime").val();
                _call(_default.get_msgId(), sendObj2, function (res) {
                    confirm_add_ok(res, config['url_page'], function () {
                        window.location.href = window.location.href;
                    });
                });

            }
        });
    }
    // $(".select2").select2();
});